package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.po.PageRequest;
import com.example.demo.po.PageResult;
import com.example.demo.po.TBizCarouse;
import com.example.demo.service.TBizCarouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tBizCarouse")
public class TBizCarouseController {

    @Autowired
    TBizCarouseService tBizCarouseService;

    @RequestMapping(value = "/list")
    public ModelAndView list( HttpServletRequest request, HttpSession session) {
        ModelAndView view = new ModelAndView();
        view.setViewName("/carouse/list");
        return  view;
    }


    @RequestMapping(value = "/getcaList")
    public String getcaList(@RequestParam(value = "page",defaultValue="1") Integer page,
                              @RequestParam(value ="limit",defaultValue="10") Integer limit,
                              @RequestParam(value ="name",defaultValue="") String name){
        JSONObject jsonObject = new JSONObject();
        try{
            PageRequest pageRequest = new PageRequest();
            pageRequest.setPageSize(limit);
            pageRequest.setPageNum(page);
            TBizCarouse ca = new TBizCarouse();
            ca.setName(name);
            PageResult resilt = tBizCarouseService.findByPage(ca,pageRequest);
            List<TBizCarouse> list = (List<TBizCarouse>) resilt.getContent();
            jsonObject.put("code",0);
            jsonObject.put("count",resilt.getTotalSize());
            jsonObject.put("data",list);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",1);
            jsonObject.put("msg","获取艺术家信息失败！");
        }
        return jsonObject.toJSONString();
    }

    @RequestMapping(value = "/init")
    public ModelAndView init(@RequestParam(value = "id",required=false) Integer id, HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        if(id!=null && !"".equals(id)){
            TBizCarouse ca = tBizCarouseService.findById(id);
            view.addObject("ca", JSON.toJSON(ca));
        }
        view.setViewName("/carouse/init");
        return  view;
    }

    @RequestMapping(value = "/edit")
    public String saveOrUpdate(@RequestBody TBizCarouse ca, HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            if(tBizCarouseService.saveOrUpdate(ca,request)){
                jsonObject.put("oper_res","success");
            }else{
                jsonObject.put("oper_res",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
        }
        return  jsonObject.toJSONString();
    }


}
